<!-- 投递标书页面 -->
<template>
    <div class="app-container">
        <el-table :data="tableData" v-loading="loading" stripe style="width: 100%">
            <el-table-column label="序号" type="index" align="center" width="50">
            </el-table-column>
            <el-table-column prop="name" label="投标人名称" align="center">
            </el-table-column>
            <el-table-column prop="person" label="联系人" align="center">
            </el-table-column>
            <el-table-column prop="phone" label="联系电话" align="center">
            </el-table-column>
            <el-table-column prop="csSubmission.tdStatus" label="投标状态" align="center">
                <template slot-scope="scope">
                    {{ getStatusText(scope.row.csSubmission.tdStatus) }}
                </template>
            </el-table-column>
            <el-table-column prop="csSubmission.tdSuccessTime" label="成功递交时间" align="center">
            </el-table-column>
            <el-table-column prop="csSubmission.address" label="文件撤回时间" align="center">
            </el-table-column>
            <el-table-column prop="csSubmission.ipAddress" label="IP地址" align="center">
            </el-table-column>
        </el-table>

        <div style="margin-top: 19px;" v-show="this.$route.query.status == 3">
            <el-button type="primary" @click="addSubmission">
                投递标书
            </el-button>
        </div>
        <el-dialog title="投递标书" :visible.sync="documentOpen" width="50%">
            <el-row :gutter="15">
                <el-form ref="elForm" :model="formData" size="medium" label-width="100px">
                    <el-col :span="12">
                        <el-form-item label="招标项目编号" prop="iNumber">
                            <el-input v-model="formData.iNumber" readonly clearable
                                :style="{ width: '100%' }"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="供应商名称" prop="hName">
                            <el-input v-model="formData.hName" readonly clearable :style="{ width: '100%' }"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="上传" prop="fjFiles" required>
                            <el-upload ref="fjFiles" :file-list="formData.fileList" action="#"
                                :on-remove="handleRemoveFile" :before-upload="uploadFileFun"
                                :http-request="uploadDeviceFile">
                                <el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
                            </el-upload>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>
            <div slot="footer">
                <el-button @click="close">取消</el-button>
                <el-button type="primary" @click="handleConfirm">确定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getBidPeopleList, csSubmission, uplodaBudgetFile, getTenderStatusTime, getPersonInfo } from '@/api/system/purchase'

export default {
    data() {
        return {
            tableData: [],
            loading: true,
            iNumber: '',
            documentOpen: false,
            formData: {
                Files: [],//这个属性用于传递数据前
                fileList: [],//用于展示
                fjFiles: '',
            },

        }
    },
    methods: {
        getStatusText(status) {
            if (status == 0) {
                return '成功';
            } else {
                return '失败'
            }
        },
        //文件上传前的校验
        uploadFileFun(file) {
            var test = /(doc|docx|pdf|txt|png|xls)$/.test(file.name.split('.').pop().toLowerCase());
            if (!test) {
                this.$message.error("请上传正确的文档格式!");
                return false;
            }
            const isLt2M = file.size / 1024 / 1024 < 10;
            if (!isLt2M) {
                this.$message.error("上传文件大小不能超过 10MB!");
                return false;
            }
        },
        getBidPeopleListss(iNumber) {
            getBidPeopleList(iNumber).then(res => {                
                this.loading = false;
                //只有招标商才能在这个页面看所有的投标人
                if (this.$store.getters.roles[0] != "admin") {
                    res.data = res.data.filter(item => {
                        return item.name === this.formData.hName
                    })
                }
                this.tableData = res.data;
            })
        },
        uploadDeviceFile(file) {
            // 创建formdata实例
            let formData = new window.FormData();
            // 将获取的文件通过append方法加入实例中
            formData.append("file", file.file);
            //发送axios请求
            uplodaBudgetFile(formData)
                .then(res => {
                    // 拿到后端响应的文件名称反显到列表的form
                    let fileInfo = {
                        name: res.data.name,
                        url: res.data.url,
                        size: res.data.size,
                        uid: file.file.uid,
                    };
                    this.formData.fileName = res.data.name;
                    if (this.formData.fileList == null) {
                        this.formData.fileList = [];
                    }
                    this.formData.fileList.push(fileInfo); // 直接添加文件名字符串
                    this.formData.Files.push(res.data)
                })
                .catch(err => {
                    console.error('上传文件失败:', err);
                });
        },
        addSubmission() {
            if (new Date() < new Date(this.$route.query.getStartTime) || new Date() > new Date(this.$route.query.getEndTime)) {
                this.$message.error("当前时间段,不可投标");
                return false;
            }
            this.formData.iNumber = this.iNumber;
            // this.formData.hName = this.$store.state.user.name
            this.documentOpen = true;
        },
        handleConfirm() {
            // 将附件转化成json
            if (new Date() >= this.$route.query.kaiStartTime) {
                this.$message.error("当前时间段,不可投标");
                return false;
            }

            if (!Array.isArray(this.formData.fileList)) {
                this.formData.fileList = [];
            }
            if (typeof this.formData.fileList === "string") {
                this.formData.fileList = JSON.parse(this.formData.fileList);
            }
            this.formData.fjFiles = JSON.stringify(this.formData.Files);
            csSubmission(this.formData).then(res => {
                if (res.data > 0) {
                    this.$message({
                        type: 'success',
                        message: '投标成功!'
                    });
                    this.getSupplierInfo();
                    this.documentOpen = false;
                    this.formData = {
                        Files: [],//这个属性用于传递数据前
                        fileList: [],//用于展示
                        fjFiles: '',
                    }
                } else {
                    this.$message({
                        type: 'warning',
                        message: '投标失败!请先获得标书'
                    });
                }
            }).catch(err => {
                this.$message({
                    type: 'warning',
                    message: '程序出错,请联系管理员'
                });
            })
        },
        //删除文件
        handleRemoveFile(file) {
            removeFile(file.name).then(res => {
                if (res.data == 1) {
                    this.$message({
                        message: '删除成功',
                        type: 'success'
                    });
                }
                const index = this.formData.fileList.findIndex(f => f.name === file.name);
                this.formData.fileList.splice(index, 1); // 从 fileList 中移除一个元素，从 index 位置开始
                this.formData.fjFiles.splice(index, 1);
            })
        },
        close() {
            this.formData = {
            };
            this.documentOpen = false;
        },
        //获得供应商信息
        getSupplierInfo() {
            getPersonInfo().then(res => {                
                if(res.data!=null){
                    this.formData.hName = res.data.hName;
                }
                this.getBidPeopleListss(this.iNumber);
            })
        },
        //专门为了获取状态和时间
        getTenderStatusTime(iNumber) {
            getTenderStatusTime(iNumber).then(res => {
                //这样设置,是因为之前采用的是路由传承,后面发现刷新不到最新值(其实每个下面mounted都有了这些值,但是最初判断并没有使用这个方法,所以为路由赋值)
                this.$route.query.status = res.data.status
                const data = res.data;
                if (data.startTime) {
                    this.$route.query.startTime = res.data.startTime
                }
                if (data.endTime) {
                    this.$route.query.endTime = res.data.endTime
                }
                if (data.kaiStartTime) {
                    this.$route.query.kaiStartTime = res.data.kaiStartTime;
                }

                if (data.kaiEndTime) {
                    this.$route.query.kaiEndTime = res.data.kaiEndTime;
                }
                if (data.getStartTime) {
                    this.$route.query.getStartTime = res.data.getStartTime
                }
                if (data.getEndTime) {
                    this.$route.query.getEndTime = res.data.getEndTime
                }
            })
        },
    },
    mounted() {
        const Iss = this.$route.query.iNumber;
        this.iNumber = Iss;
        // this.getBidPeopleListss(Iss);
        this.getTenderStatusTime(Iss);
    },
    created(){
        this.getSupplierInfo();
    },

}
</script>
<style scoped></style>
